<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #fullscreen-container {
      width: 200px;
      height: 200px;
      background-color: deeppink;
    }
  </style>
</head>

<body>
  <button id="fullscreen-button">全屏</button>
  <div id="fullscreen-container">
    <div style="width: 100px;height: 100px;background-color: pink;"></div>
  </div>
  <script>
    var fullscreenButton = document.getElementById("fullscreen-button");
    var fullscreenContainer = document.getElementById("fullscreen-container");

    // 进入全屏
    function enterFullscreen() {
      // 移除按钮和容器的固定定位样式
      fullscreenButton.style.position = "static";
      fullscreenContainer.style.position = "static";

      // 设置容器的宽高为浏览器可视区域的宽高
      fullscreenContainer.style.width = window.innerWidth + "px";
      fullscreenContainer.style.height = window.innerHeight + "px";

      // 设置容器的层级为最高
      fullscreenContainer.style.zIndex = "9999";

      // 隐藏滚动条
      document.body.style.overflow = "hidden";
    }

    // 退出全屏
    function exitFullscreen() {
      // 还原按钮和容器的样式
      fullscreenButton.style.position = "";
      fullscreenContainer.style.position = "";

      // 清空容器的宽高
      fullscreenContainer.style.width = "";
      fullscreenContainer.style.height = "";

      // 还原容器的层级
      fullscreenContainer.style.zIndex = "";

      // 显示滚动条
      document.body.style.overflow = "";
    }

    // 监听按钮点击事件
    fullscreenButton.addEventListener("click", function () {
      enterFullscreen();
    });

    // 监听按键事件
    document.addEventListener("keydown", function (e) {
      if (e.key === "Escape" || e.key === "Esc") {
        exitFullscreen();
      }
    });
  </script>
</body>

</html>